#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python常用模块库 
@File ：winservice.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/2/21 上午8:58 
"""

import win32event
import win32service
import win32serviceutil


class WinService(win32serviceutil.ServiceFramework):
	_svc_name_ = "FileMonitorService"
	_svc_display_name_ = "File System Monitor"
	_svc_description_ = "Monitors and manages file system usage"

	def __init__(self, args):
		super().__init__(args)
		self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
		self.is_alive = True

	def SvcStop(self):
		self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
		win32event.SetEvent(self.hWaitStop)
		self.is_alive = False

	def SvcDoCommand(self):
		from ..core.monitor import FileMonitor
		monitor = FileMonitor.load_default()
		while self.is_alive:
			monitor.check_thresholds()
			win32event.WaitForSingleObject(self.hWaitStop, 5000)
